import java.util.concurrent.CountDownLatch;

public class BitTorrent {
	public SharedMessageQueue sharedMsg[];
	public CountDownLatch countDown;

	public static void main(String[] args) {
		if (args.length == 0) {
			System.out.println("No peerID given!");
			System.exit(0);
		}

		// read command line argument for peerID
		int peerID = Integer.parseInt(args[0]);
		
		// Read File into FileProperties
		FileProperties fileProp = new FileProperties(true);

		// Initialize Peer data
		PeerData peerData = new PeerData(fileProp, peerID);

		/*********** Initializing Shared Object *******************************/
		BitTorrent bitTorrent = new BitTorrent();
		bitTorrent.sharedMsg = new SharedMessageQueue[fileProp.peerIDArray.length - 1];

		// Countdown latch initialize
		bitTorrent.countDown = new CountDownLatch(
				fileProp.peerIDArray.length - 1);
		/*********** Initialize Logger ********************/
		Logger logger = new Logger(peerData);

		/********** Initializing sender and receiver threads ******************/
		Thread socConn = new SocketConnections(fileProp, peerData,
				bitTorrent.sharedMsg, bitTorrent.countDown, logger);
		socConn.start();

		/***********
		 * Common thread for calculating prefered neighbours irrespective of
		 * peer connected to
		 *********/
		Thread prefNeig = new PrefNeighbourCalThread(peerData,
				bitTorrent.sharedMsg);
		prefNeig.start();
	}
}
